package com.zhanyd.app.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhanyd.app.common.ApiResult;
import com.zhanyd.app.common.util.DateHelp;
import com.zhanyd.app.common.util.JwtUtils;
import com.zhanyd.app.common.util.StringHelp;
import com.zhanyd.app.model.ProjectInfo;
import com.zhanyd.app.service.ProjectService;
import com.zhanyd.app.service.UserService;


@RestController
@EnableAutoConfiguration
@RequestMapping("/project")
public class ProjectController {

	@Autowired
	ProjectService projectService;
	
	@Autowired
    UserService userInfoService;
	
	/**
	   * 获取项目列表
	 * @param projectNum
	 * @param developer
	 * @param building
	 * @param householderName
	 * @param address
	 * @param tel
	 * @param isUrgent
	 * @param managentProgress
	 * @param paymentProgress
	 * @param deliveryProgress
	 * @param startTime
	 * @param endTime
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("/getProjectList")
    public ApiResult<PageInfo<Map<String,Object>>> getProjectList(String projectNum,String developer,String building,String householderName,
    		String address,String tel,Boolean isUrgent,String managentProgress,String paymentProgress,String deliveryProgress,String startTime,String endTime,String pageSize,String pageNum){
		ApiResult<PageInfo<Map<String,Object>>> apiResult = new ApiResult<PageInfo<Map<String,Object>>>();
		PageHelper.startPage(Integer.parseInt(null == pageNum ? "1" : pageNum), Integer.parseInt(null == pageSize ? "10" : pageSize));
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("projectNum", projectNum);
		param.put("developer", developer);
		param.put("building", building);
		param.put("householderName", householderName);
		param.put("address", address);
		param.put("tel", tel);
		param.put("isUrgent", isUrgent);
		param.put("managentProgress", managentProgress);
		param.put("paymentProgress", paymentProgress);
		param.put("deliveryProgress", deliveryProgress);
		param.put("startTime", startTime);
		if (endTime != null && endTime != "") {
			param.put("endTime", DateHelp.addOneDay(endTime));
		}
    	List<Map<String,Object>> projectList = projectService.selectByParam(param);
    	PageInfo<Map<String,Object>> page = new PageInfo<Map<String,Object>>(projectList);
    	return apiResult.success(page);
	}
	
	
	/**
	 * 
	 * 新增或编辑项目
	 * @param projectInfo
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateProjectInfo")
	public ApiResult<String> updateProjectInfo(ProjectInfo projectInfo,HttpServletRequest request){
		ApiResult<String> apiResult = new ApiResult<String>();
		int count = 0;
		String token = request.getHeader("Authorization");
		Integer UserId = JwtUtils.verifyJWT(token);
		if(projectInfo.getId() == null){
			projectInfo.setCreateBy(UserId);
			projectInfo.setCreateTime(new Date());
			count = projectService.insertSelective(projectInfo);
		}else {
			projectInfo.setUpdateBy(UserId); 
			count = projectService.updateByPrimaryKeySelective(projectInfo);
		}
		
		if(count > 0) {
			return apiResult.success("保存成功");
		}else {
			return apiResult.fail("保存失败");
		}
	}
	
	/**
	 * 获取项目数量
	 * @param isUrgent
	 * @return
	 */
	@RequestMapping("/getProjectNum")
	public ApiResult<Integer> getProjectNum(Boolean isUrgent,HttpServletRequest request){
		ApiResult<Integer> apiResult = new ApiResult<Integer>();
		Map<String, Object> param = new HashMap<String, Object>();
		String token = request.getHeader("Authorization");
		Integer userId = JwtUtils.verifyJWT(token);
		List<Map<String, Object>> roleMapList = userInfoService.selectRoleInfoByUserId(userId);
    	if(!roleMapList.isEmpty()){
    		//如果不是管理员，传入开发商的名字
    		if(!"admin".equals(StringHelp.valueOf(roleMapList.get(0).get("role_name")))){
    			param.put("developer", roleMapList.get(0).get("role_name"));
    		}
    	}
		
		param.put("isUrgent", isUrgent);
		Integer count = projectService.selectProjectNum(param);
		return apiResult.success(count);
	}
	
	
	/**
	 * 获取所有项目信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getAllProject")
	public ApiResult<List<Map<String,Object>>> getAllProject(HttpServletRequest request){
		ApiResult<List<Map<String,Object>>> apiResult = new ApiResult<List<Map<String,Object>>>();
		Map<String, Object> param = new HashMap<String, Object>();
		String token = request.getHeader("Authorization");
		Integer userId = JwtUtils.verifyJWT(token);
		List<Map<String, Object>> roleMapList = userInfoService.selectRoleInfoByUserId(userId);
    	if(!roleMapList.isEmpty()){
    		//如果不是管理员，传入开发商的名字
    		if(!"admin".equals(StringHelp.valueOf(roleMapList.get(0).get("role_name")))){
    			param.put("developer", roleMapList.get(0).get("role_name"));
    		}
    	}
		
		List<Map<String,Object>> projectList = projectService.selectByParam(param);
		return apiResult.success(projectList);
	}
}
